<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>员工管理</title>
    <style>
        .action-buttons .btn {
            margin-right: 5px;
        }
        .modal-header {
            background-color: #007bff;
            color: white;
        }
        .table-actions {
            width: 150px;
            text-align: center;
        }
    </style>
</head>
<body>
<jsp:include page="layout/header.jsp"/>
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">员工管理</h5>
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addEmpModal">
                        <i class="fas fa-plus"></i> 新增员工
                    </button>
                </div>
                <div class="card-body">
                    <!-- 搜索表单 -->
                    <form class="form-inline mb-4" action="list.do" method="get">
                        <div class="input-group">
                            <input class="form-control" type="text" name="name" placeholder="搜索员工姓名" value="${name}"/>
                            <div class="input-group-append">
                                <button class="btn btn-primary" type="submit">
                                    <i class="fas fa-search"></i> 查询
                                </button>
                                <a class="btn btn-secondary" href="list.do">
                                    <i class="fas fa-times"></i> 清空
                                </a>
                            </div>
                        </div>
                    </form>
                    
                    <!-- 错误提示 -->
                    <c:if test="${not empty error}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            ${error}
                            <button type="button" class="close" data-dismiss="alert">
                                <span>&times;</span>
                            </button>
                        </div>
                    </c:if>
                    
                    <!-- 数据表格 -->
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="thead-light">
                                <tr>
                                    <th>ID</th>
                                    <th>姓名</th>
                                    <th>用户名</th>
                                    <th>性别</th>
                                    <th>手机号</th>
                                    <th>部门</th>
                                    <th>入职时间</th>
                                    <th class="table-actions">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach var="e" items="${list}">
                                    <tr>
                                        <td>${e.id}</td>
                                        <td>${e.name}</td>
                                        <td>${e.username}</td>
                                        <td><c:out value="${e.gender == 1 ? '男' : '女'}"/></td>
                                        <td>${e.phone}</td>
                                        <td>${e.deptName}</td>
                                        <td>${e.entryDate}</td>
                                        <td class="action-buttons">
                                            <button type="button" class="btn btn-sm btn-primary" 
                                                    onclick="editEmp(${e.id}, '${e.name}', '${e.username}', '${e.phone}', ${e.deptId}, ${e.gender}, ${e.job}, ${e.salary}, '${e.entryDate}', '${e.image}')" 
                                                    data-toggle="modal" data-target="#editEmpModal">
                                                <i class="fas fa-edit"></i> 编辑
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" 
                                                    onclick="deleteEmp(${e.id}, '${e.name}')">
                                                <i class="fas fa-trash"></i> 删除
                                            </button>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-3">
                        <a class="btn btn-secondary" href="../pages/main.jsp">
                            <i class="fas fa-arrow-left"></i> 返回首页
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 新增员工弹窗 -->
<div class="modal fade" id="addEmpModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">新增员工</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="add.do" method="post">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpName">姓名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addEmpName" name="name" 
                                       placeholder="请输入员工姓名" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpUsername">用户名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addEmpUsername" name="username" 
                                       placeholder="请输入用户名" required>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpGender">性别 <span class="text-danger">*</span></label>
                                <select class="form-control" id="addEmpGender" name="gender" required>
                                    <option value="">请选择性别</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpPhone">手机号</label>
                                <input type="text" class="form-control" id="addEmpPhone" name="phone" 
                                       placeholder="请输入手机号">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpJob">职位</label>
                                <select class="form-control" id="addEmpJob" name="job">
                                    <option value="">请选择职位</option>
                                    <option value="1">经理</option>
                                    <option value="2">员工</option>
                                    <option value="3">实习生</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpSalary">薪资</label>
                                <input type="number" class="form-control" id="addEmpSalary" name="salary" 
                                       placeholder="请输入薪资" min="0">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpDept">部门 <span class="text-danger">*</span></label>
                                <select class="form-control" id="addEmpDept" name="deptId" required>
                                    <option value="">请选择部门</option>
                                    <c:forEach var="d" items="${depts}">
                                        <option value="${d.id}">${d.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addEmpEntryDate">入职日期</label>
                                <input type="date" class="form-control" id="addEmpEntryDate" name="entryDate">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="addEmpImage">头像URL</label>
                        <input type="url" class="form-control" id="addEmpImage" name="image" 
                               placeholder="请输入头像URL（可选）">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-success">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑员工弹窗 -->
<div class="modal fade" id="editEmpModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">编辑员工</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="update.do" method="post">
                <input type="hidden" id="editEmpId" name="id">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpName">姓名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editEmpName" name="name" 
                                       placeholder="请输入员工姓名" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpUsername">用户名 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editEmpUsername" name="username" 
                                       placeholder="请输入用户名" required>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpGender">性别 <span class="text-danger">*</span></label>
                                <select class="form-control" id="editEmpGender" name="gender" required>
                                    <option value="">请选择性别</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpPhone">手机号</label>
                                <input type="text" class="form-control" id="editEmpPhone" name="phone" 
                                       placeholder="请输入手机号">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpJob">职位</label>
                                <select class="form-control" id="editEmpJob" name="job">
                                    <option value="">请选择职位</option>
                                    <option value="1">经理</option>
                                    <option value="2">员工</option>
                                    <option value="3">实习生</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpSalary">薪资</label>
                                <input type="number" class="form-control" id="editEmpSalary" name="salary" 
                                       placeholder="请输入薪资" min="0">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpDept">部门 <span class="text-danger">*</span></label>
                                <select class="form-control" id="editEmpDept" name="deptId" required>
                                    <option value="">请选择部门</option>
                                    <c:forEach var="d" items="${depts}">
                                        <option value="${d.id}">${d.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editEmpEntryDate">入职日期</label>
                                <input type="date" class="form-control" id="editEmpEntryDate" name="entryDate">
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="editEmpImage">头像URL</label>
                        <input type="url" class="form-control" id="editEmpImage" name="image" 
                               placeholder="请输入头像URL（可选）">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script>
    function editEmp(id, name, username, phone, deptId, gender, job, salary, entryDate, image) {
        document.getElementById('editEmpId').value = id;
        document.getElementById('editEmpName').value = name;
        document.getElementById('editEmpUsername').value = username;
        document.getElementById('editEmpPhone').value = phone || '';
        document.getElementById('editEmpDept').value = deptId || '';
        document.getElementById('editEmpGender').value = gender || '';
        document.getElementById('editEmpJob').value = job || '';
        document.getElementById('editEmpSalary').value = salary || '';
        document.getElementById('editEmpEntryDate').value = entryDate || '';
        document.getElementById('editEmpImage').value = image || '';
    }
    
    function deleteEmp(id, name) {
        if (confirm('确认删除员工 "' + name + '" 吗？')) {
            window.location.href = 'delete.do?id=' + id;
        }
    }
</script>

<jsp:include page="layout/footer.jsp"/>
</body>
</html>

